package com.fy.fyspace.common.utils;

import com.aliyun.oss.*;
import com.aliyun.oss.model.BucketStat;
import com.aliyun.oss.model.PutObjectRequest;
import com.fy.fyspace.common.config.AliOssConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Component
public class AliOssUtil {

    @Autowired
    private AliOssConfig aliOssConfig;

    @Resource
    private OSS ossClient;

    /**
     * 上传图片
     */
    public String uploadImg(MultipartFile file) {
        try {
            // 将 MultipartFile 转换为字节数组，这样可以多次使用
            byte[] fileBytes = file.getBytes();

            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();

            // 使用字节数组输入流代替直接使用 file.getInputStream()
            InputStream inputStream = new ByteArrayInputStream(fileBytes);

            PutObjectRequest putObjectRequest = new PutObjectRequest(aliOssConfig.getBucketName(), fileName, inputStream);
            ossClient.putObject(putObjectRequest);

            // 生成文件访问路径
            return getFileUrl(fileName);

        } catch (IOException e) {
            log.error("上传图片到OSS失败", e);
            throw new RuntimeException("上传图片失败：" + e.getMessage());
        }
    }

    /**
     * 文件上传
     */
    public String upload(byte[] bytes, String objectName) {
        try {
            if (ossClient.doesObjectExist(aliOssConfig.getBucketName(), objectName)) {
                ossClient.deleteObject(aliOssConfig.getBucketName(), objectName);
                log.info("删除已存在的文件: {}", objectName);
            }

            ossClient.putObject(aliOssConfig.getBucketName(), objectName, new ByteArrayInputStream(bytes));
            
            return getFileUrl(objectName);
        } catch (OSSException | ClientException e) {
            log.error("上传文件时发生错误", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除云存储数据
     */
    public void delete(String objectName) {
        try {
            ossClient.deleteObject(aliOssConfig.getBucketName(), objectName);
            log.info("删除文件: {}", objectName);
        } catch (OSSException | ClientException e) {
            log.error("删除文件时发生错误", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取OSS空间使用信息
     */
    public BucketStat getBucketStat() {
        try {
            return ossClient.getBucketStat(aliOssConfig.getBucketName());
        } catch (OSSException | ClientException e) {
            log.error("获取存储桶统计信息时发生错误", e);
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 获取文件URL
     */
    public String getFileUrl(String objectName) {
        return String.format("https://%s.%s/%s", 
                aliOssConfig.getBucketName(), 
                aliOssConfig.getEndpoint(), 
                objectName);
    }
} 